﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using CDSBDemandControl.Model;

namespace CDSBDemandControlServiceAux.Translator
{
    class XMLModelTranslateAux
    {
        public XMLModelTranslateAux()
        { }

        public static ScenarioModel TranslateScenario(WorkItem workItem, string related)
        {
            ScenarioModel mod = new ScenarioModel();
            mod.ID = workItem.Id;
            mod.Project = workItem.Project.Name;
            mod.Title = workItem.Title;
            mod.AssignedTo = workItem.Fields.Contains("Assigned To") && workItem.Fields["Assigned To"].Value != null ? workItem.Fields["Assigned To"].Value.ToString() : "";
            mod.Related = related;
            mod.Status = workItem.State;

            return mod;

        }

        public static void AddTaskDesenvToScenario(ScenarioModel parent, WorkItem child)
        {
            TaskDesenvModel neo = new TaskDesenvModel();

            neo.ID = child.Id;
            neo.Title = child.Title;

            if (child.Fields.Contains("Inicio Realizado") && child.Fields["Inicio Realizado"].Value != null)
            { 
                neo.Start = DateTime.Parse(child.Fields["Inicio Realizado"].Value.ToString()) ;
            }
            else
                neo.Start =  null;

            if(child.Fields.Contains("Termino Realizado") && child.Fields["Termino Realizado"].Value != null)
            { 
                neo.Conclude = DateTime.Parse(child.Fields["Termino Realizado"].Value.ToString()); 
            }
            else
                neo.Conclude = null;

            if (child.Fields.Contains("Start Date") && child.Fields["Start Date"].Value != null)
            {
                neo.EstimatedStart = DateTime.Parse(child.Fields["Start Date"].Value.ToString());
            }
            else
                neo.EstimatedStart = null;

            if (child.Fields.Contains("Finish Date") && child.Fields["Finish Date"].Value != null)
            {
                DateTime.Parse(child.Fields["Finish Date"].Value.ToString());
            }
            else
                neo.EstimatedConclude = null;
            
            neo.AssignedTo = child.Fields.Contains("Assigned To") && child.Fields["Assigned To"].Value != null ? child.Fields["Assigned To"].Value.ToString() : string.Empty;
            neo.Status = child.State;

            parent.RelatedLinks.Add(neo);
        }
    }
}